package com.extr.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.extr.controller.domain.QuestionQueryResult;
import com.extr.domain.question.Field;
import com.extr.domain.question.UserQuestionHistory;
import com.extr.security.UserInfo;
import com.extr.service.ExamService;
import com.extr.service.PracticeService;
import com.extr.service.QuestionService;
import com.extr.service.UserService;
import com.extr.util.QuestionAdapter;

/**
 *项目：广发网
 *版权：Copyright(c)2013
 *公司：广发证券股份有限公司
 *作者：胡纬
 *创建日期：2015-10-21
 *创建时间：下午3:54:27
 *功能描述：
 */
@Controller
public class MobilePracticeController {
    @Autowired
    private ExamService examService;
    @Autowired
    private UserService userService;
    @Autowired
    private PracticeService practiceService;
    @Autowired
    private QuestionService questionService;
    
    //随机练习返回结果集
    @RequestMapping(value = "/mobile/practice_random", method = RequestMethod.GET)
    @ResponseBody
    public List<QuestionQueryResult> practiceRandom(HttpServletRequest request) {
        List<Integer> fieldIdList = new ArrayList<Integer>();
        fieldIdList.add(1);
        List<Integer> typeIdList = new ArrayList<Integer>();
        typeIdList.add(1);
        typeIdList.add(2);
        typeIdList.add(3);
        typeIdList.add(4);        
        List<QuestionQueryResult> qqrList = questionService.getQuestionQueryResultListByFieldIdList(fieldIdList,typeIdList, 20);        
        return qqrList;
    }
    
    @RequestMapping(value = "/mobile/page_random", method = RequestMethod.GET)
    public String pageRandom(Model model) {
        return "mobile/random";
    }
    
    
    //错题返回结果集
    @RequestMapping(value = "/mobile/practice_incorrect", method = RequestMethod.GET)
    @ResponseBody
    public List<QuestionQueryResult> practiceIncorrect(HttpServletRequest request) {
        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        UserQuestionHistory uqh = questionService.getUserQuestionHistoryByUserId(userInfo.getUserid());        
        List<Integer> idList = new ArrayList<Integer>();
        if(uqh != null){
            if(uqh.getHistory().containsKey(0)){
                Iterator<Integer> it = uqh.getHistory().get(0).keySet().iterator();
                while(it.hasNext()){
                    idList.add(it.next());
                }
            }
        }
        List<QuestionQueryResult> qqrList = new ArrayList<QuestionQueryResult>();
        if(uqh != null && uqh.getHistory() != null && uqh.getHistory().containsKey(0))
            qqrList = examService.getQuestionDescribeListByIdList(idList);   
        return qqrList;
    }
    
    @RequestMapping(value = "/mobile/page_incorrect", method = RequestMethod.GET)
    public String pageIncorrect(Model model) {
        return "mobile/incorrect";
    }

}
